﻿/* DotNetHoldem 
 * .Net Solution that allows to use OpenHoldem with .Net logic
 * Author: Óscar Andreu
 * oesido at gmail dot com
 * Licensed under GPLv3 - 2012
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetBotLogic.Classes.Log;
using DotNetBotLogic.Controllers;

namespace DotNetBotLogic.Classes.PlayHistory
{
    internal class PreflopTracker : TrackerBase
    {
        private int[] preflopOrder = new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 };

        #region Constructor

        internal PreflopTracker(PlayTrackerController ctrl)
            : base(ctrl)
        { }

        #endregion

        internal void TrackData()
        {
            //Deal positions goes from 1 to 11 (max) but we are taking int[] preflop order
            for (int i = 0; i < 10; i++)
            {
                player = ctrl.PlayerList.Where(a => a.DealPosition == preflopOrder[i]).FirstOrDefault();
                if (player == null || player.Fold)
                    continue;

                betPosition = (int)OhHelper.GetSymbol(string.Format("ac_betpos{0}", player.CurrentChair));
                balance = OhHelper.GetSymbol(string.Format("balance{0}", player.CurrentChair));

                // if betposition == 0 the player isn't playing and if player.Balance == balance there is no action to track
                if (betPosition > 0)
                {
                    if (player.Balance == balance)
                        continue;

                    player.Balance = balance;
                    currentBet = OhHelper.GetSymbol(string.Format("currentbet{0}", player.CurrentChair));
                    if (currentBet > ctrl.MaxBet)
                    {
                        if (ctrl.NumberOfPFBets == 0)
                        {
                            player.PFR++;
                            Logger.Log(string.Format("PlayTracker PF -- {0} position:{1} Raise: {2}", player.Name, player.DealPosition, currentBet-ctrl.MaxBet));
                        }
                        else if (ctrl.NumberOfPFBets == 1)
                        {
                            player._3B++;
                            Logger.Log(string.Format("PlayTracker PF -- {0} position:{1} 3B: {2}", player.Name, player.DealPosition, currentBet - ctrl.MaxBet));
                        }
                        else if (ctrl.NumberOfPFBets == 2 || (player.Balance == 0 && currentBet > BaseController.BBlind * 25))
                        {
                            player._4B++;
                            Logger.Log(string.Format("PlayTracker PF -- {0} position:{1} AllIn: {2}", player.Name, player.DealPosition, currentBet - ctrl.MaxBet));
                        }
                        else if (ctrl.NumberOfPFBets > 2 || (player.Balance == 0 && currentBet > BaseController.BBlind * 25))
                        {
                            player.PF_Alli++;
                            Logger.Log(string.Format("PlayTracker PF -- {0} position:{1} AllIn: {2}", player.Name, player.DealPosition, currentBet - ctrl.MaxBet ));
                        }

                        ctrl.MaxBet = currentBet;
                        ctrl.NumberOfPFBets++;
                        player.sta_VP_IP++;
                    }
                    else if (currentBet == ctrl.MaxBet && !(player.DealPosition == 2 && currentBet == BaseController.BBlind))
                    {
                        player.PFC++;
                        player.sta_VP_IP++;
                        Logger.Log(string.Format("PlayTracker PF -- {0} position:{1} Call", player.Name, player.DealPosition, currentBet));
                    }
                }
                else
                {
                    if (!player.Fold)
                    {
                        if (ctrl.NumberOfPFBets == 0)
                        {
                            player.PFF++;
                        }
                        else if (ctrl.NumberOfPFBets == 1)
                        {
                            player.F3B++;
                        }
                        else if (ctrl.NumberOfPFBets == 2 || (player.Balance == 0 && currentBet > BaseController.BBlind * 25))
                        {
                            player.PF_FoldAlli++;
                        }

                        player.SetFold(MainController.BetRound);
                        ctrl.NumPlayers--;

                        Logger.Log(string.Format("PlayTracker PF -- {0} position:{1} FOLD", player.Name, player.DealPosition));
                    }
                }

            }
        }


    }
}
